package com.mkvld.appietijdenv2.fragments;

import com.mkvld.appietijdenv2.data.Payment;
import com.mkvld.appietijdenv2.data.Preferences;
import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.data.Utils;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragPayrollV9 extends PeriodFragV9 {
	Payment payroll;
	double hourLoan;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(getLayoutId(), null, false);
		update(v);
		return v;
	}
	
	protected int getLayoutId() {
		return R.layout.frag_payroll;
	}

	

	private void setRow(View root, PayrollPart payrollPart) {
		setHourLoanText(root, payrollPart, hourLoan);
		if (payrollPart.hours > 0) {
			showRow(root, payrollPart);
		} else {
			hideRow(root, payrollPart);
		}
	}
	
	private static void setHourLoanText(View root, PayrollPart payrollPart, double hourlyLoan) {
		if (payrollPart.hourId > -1)
			((TextView)root.findViewById(payrollPart.hourId)).setText(Utils.timeToString(payrollPart.hours) + " uur");
		((TextView)root.findViewById(payrollPart.loanId))
			.setText(String.format("€ %.2f", payrollPart.hours * hourlyLoan));
	}
	
	private void hideRow(View root, PayrollPart payrollPart) {
		if (!payrollPart.alwaysVisible) {
			root.findViewById(payrollPart.lblId).setVisibility(View.GONE);
			root.findViewById(payrollPart.hourId).setVisibility(View.GONE);
			root.findViewById(payrollPart.loanId).setVisibility(View.GONE);
		}
	}
	private void showRow(View root, PayrollPart payrollPart) {
		root.findViewById(payrollPart.lblId).setVisibility(View.VISIBLE);
		if (payrollPart.hoursVisible)
			root.findViewById(payrollPart.hourId).setVisibility(View.VISIBLE);
		root.findViewById(payrollPart.loanId).setVisibility(View.VISIBLE);
	}

	private void update(View root) {
		((TextView)root.findViewById(getTitleViewId())).setText(getResources().getString(R.string.fp_title, getPeriod() + 1, getYear()));
		payroll = new Payment(getActivity(), getYear(), getPeriod());
		hourLoan = Preferences.getLoan(getActivity());
		for (PayrollPart payrollPart : getPayrollParts()) {
			setRow(root, payrollPart);
		}
	}
	
	protected int getTitleViewId() {
		return R.id.fp_title;
	}
	
	protected PayrollPart[] getPayrollParts() {
		double total = payroll.data[Payment.WORKED]
				+ payroll.data[Payment.SICK]
				+ payroll.data[Payment.BD]
				+ payroll.data[Payment.ALLOWANCE]
				+ payroll.data[Payment.VACATION_PAYED]
				+ payroll.data[Payment.ADV_PAYED]
				+ payroll.data[Payment.TVT_PAYED];
		return new PayrollPart[] {
			new PayrollPart(payroll.data[Payment.WORKED], R.id.fp_worked_lbl, R.id.fp_worked_hours, R.id.fp_worked_loan, true, true),
			new PayrollPart(payroll.data[Payment.ALLOWANCE], R.id.fp_allowance_lbl, R.id.fp_allowance_hours, R.id.fp_allowance_loan, false, true),
			new PayrollPart(payroll.data[Payment.SICK], R.id.fp_sick_lbl, R.id.fp_sick_hours, R.id.fp_sick_loan, false, true),
			new PayrollPart(payroll.data[Payment.BD], R.id.fp_bd_lbl, R.id.fp_bd_hours, R.id.fp_bd_loan, false, true),
			new PayrollPart(payroll.data[Payment.TVT_PAYED], R.id.fp_tvt_lbl, R.id.fp_tvt_hours, R.id.fp_tvt_loan, false, true),
			new PayrollPart(payroll.data[Payment.ADV_PAYED], R.id.fp_adv_lbl, R.id.fp_adv_hours, R.id.fp_adv_loan, false, true),
			new PayrollPart(payroll.data[Payment.VACATION_PAYED], R.id.fp_vacaction_lbl, R.id.fp_vacation_hours, R.id.fp_vacation_loan, false, true),
			new PayrollPart(total, R.id.fp_gross_lbl, R.id.fp_gross_hours, R.id.fp_gross_loan, true, false)
		};
	}



	@Override
	public void update() {
		if (getView() != null)
			update(getView());
	}
	
	protected static class PayrollPart {
		double hours; int lblId, hourId, loanId; boolean alwaysVisible, hoursVisible;
		PayrollPart(double hours, int lblId, int hourId, int loadId, boolean alwaysVisible, boolean hoursVisible) {
			this.hours = hours; this.lblId = lblId; this.hourId = hourId; this.loanId = loadId; this.alwaysVisible = alwaysVisible; this.hoursVisible = hoursVisible;
		}
		
	}

}
